import cn.zhxu.okhttps.HTTP;
import cn.zhxu.okhttps.HttpResult;
import cn.zhxu.okhttps.gson.GsonMsgConvertor;
import com.google.gson.JsonObject;

public class TryOnClothesReqUtils {
    public static final String API_KEY = "sk-xxxx";//你的APIkey 

    public static final String SUMIT_BIZ_URL = "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2image/image-synthesis";

    /**
     * 作业提交接口
     * POST https://dashscope.aliyuncs.com/api/v1/services/aigc/image2image/image-synthesis
     * curl --location 'https://dashscope.aliyuncs.com/api/v1/services/aigc/image2image/image-synthesis/' \
     * --header 'X-DashScope-Async: enable' \
     * --header 'Authorization: Bearer <YOUR_API_KEY>' \
     * --header 'Content-Type: application/json' \
     * --data '{
     *     "model": "aitryon",
     *     "input": {
     *         "top_garment_url": "http://xxx/1.jpg",
     *         "bottom_garment_url": "http://xxx/2.jpg",
     *         "person_image_url": "http://xxx/3.jpg"
     *     },
     *     "parameters": {
     *         "resolution": -1,
     *         "restore_face": true
     *     }
     *   }'
     */
    public static void submitBiz(TryOnClothesReq req){
        /*HttpResult post = OkHttps.sync(SUMIT_BIZ_URL)
                .addHeader("Authorization", "Bearer " + API_KEY)
                .bodyType("json")
                .addBodyPara("model", "aitryon")
                .addBodyPara("input.top_garment_url", req.getTop_garment_url()).post();
        System.out.println("------------>"+post.getBody().toString());*/

        String params = "{ " +
                "        \"model\": \"aitryon\", " +
                "        \"input\": { " +
                "          \"top_garment_url\": \""+req.getTop_garment_url()+"\", " +
                "          \"bottom_garment_url\": \""+req.getBottom_garment_url()+"\", " +
                "          \"person_image_url\": \""+req.getPerson_image_url()+"\" " +
                "        }, " +
                "        \"parameters\": { " +
                "          \"resolution\": -1, " +
                "         \" restore_face\": true " +
                "        } " +
                "      } ";
        //JSONObject jsonParams = JSONObject.fromObject(params);
      /*  JSONObject jsonParams = new JSONObject();
        jsonParams.put("top_garment_url", req.getTop_garment_url());
        jsonParams.put("person_image_url", req.getBottom_garment_url());*/

        //System.out.println("jsonParams------------>"+jsonParams.toString());
        // 创建HTTP实例
        HTTP http = HTTP.builder()
                .addMsgConvertor(new GsonMsgConvertor())
                .build();

  /*      Gson gson = new Gson();
        JsonObject asJsonObject = gson.toJsonTree(params).getAsJsonObject();*/
//        JsonObject asJsonObject = JsonParser.parseString(params).getAsJsonObject();
        JsonObject pj = new JsonObject();
        pj.addProperty("model","aitryon");

        http.async(SUMIT_BIZ_URL)
                .addHeader("X-DashScope-Async", "enable")
                .addHeader("Authorization", "Bearer " + API_KEY)
                .addHeader("Content-Type", "application/json")
                .bodyType("json")
                .setBodyPara(pj)
                .setOnResponse((HttpResult res) -> {

                        System.out.println("=======================>"+res.getBody());
                        System.out.println("=======================>"+res.getStatus());
                }).post();



    }

    public static void main(String[] args) {
        TryOnClothesReq req = new TryOnClothesReq();
        req.setTop_garment_url("123");
        req.setPerson_image_url("456");
        req.setBottom_garment_url("789");
        submitBiz(req);

    }
}
